package cn.wolfcode.service.Impl;
import cn.wolfcode.domain.Result;
import cn.wolfcode.mapper.EmpMapper;
import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.PageBean;
import cn.wolfcode.service.EmpService;
import cn.wolfcode.util.JwtUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageBean query(int page, int pageSize, String name, Integer gender, Date begin, Date end) {
        PageHelper.startPage(page,pageSize);
        List<Emp> emps = empMapper.list(name,gender,begin,end);
        Page<Emp> p = (Page<Emp>) emps;
        return new PageBean(p.getTotal(),p.getResult());
    }

    @Override
    public void batchDelete(List<Long> ids) {
        empMapper.batchDelete(ids);
    }

    @Override
    public void save(Emp emp) {
        empMapper.save(emp);
    }

    @Override
    public Emp selById(Long id) {
        return empMapper.selById(id);
    }

    @Override
    public void update(Emp emp) {
        empMapper.update(emp);
    }

    @Override
    public Result login(Emp emp) {
        Emp logined = empMapper.login(emp);
        if (logined != null) {
            Map<String , Object> map = new HashMap<>();
            map.put("id",logined.getId());
            map.put("username",logined.getUsername());
            return Result.success(JwtUtils.generateToken(map));
        }else {
            return Result.error("登录失败");
        }

    }


}
